/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ModulePropertyInternalFrame.java
 *
 * Created on 18.05.2011, 17:19:45
 */
package com.googlecode.snoopycp.ui;

import com.googlecode.snoopycp.core.Domain;
import com.googlecode.snoopycp.model.ParamTableModel;
import com.googlecode.snoopycp.model.TimeTableModel;
import org.apache.log4j.Logger;


public class ModulePropertyInternalFrame extends javax.swing.JInternalFrame {

    /** Creates new form ModulePropertyInternalFrame */
    public static final Logger logger = Logger.getLogger(ModulePropertyInternalFrame.class);
    Domain domain;
    Ice.Identity ident;
    String muid;

    public ModulePropertyInternalFrame(Domain _domain, Ice.Identity _ident, String _muid) {
        initComponents();
        domain = _domain;
        ident = _ident;
        muid = _muid;

        this.jTable2.setModel(new TimeTableModel(_domain, _ident, logger, _muid));
        this.jTable1.setModel(new ParamTableModel(_domain, _ident, logger, _muid));

        initStatus();
        this.setClosable(true);
        this.setTitle(domain.moduleName(_ident).get(muid) + "properties");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnON = new javax.swing.JToggleButton();
        btnOFF = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        btnON.setText("ON");
        btnON.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnONActionPerformed(evt);
            }
        });

        btnOFF.setText("OFF");
        btnOFF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOFFActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("Module status");

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null}
            },
            new String [] {
                "Title 1"
            }
        ));
        jScrollPane1.setViewportView(jTable2);

        jTabbedPane1.addTab("Schedule", jScrollPane1);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null}
            },
            new String [] {
                "Title 1"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        jTabbedPane1.addTab("Parameters", jScrollPane2);

        jButton1.setText("Close properties");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(64, 64, 64)
                        .addComponent(btnON, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(btnOFF)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(btnON)
                    .addComponent(btnOFF))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOFFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOFFActionPerformed
        if (this.btnON.isSelected()) {
            this.btnOFF.setSelected(true);
            this.btnON.setSelected(false);
        }
        // TODO if node is dead
        this.domain.scheduler(ident).toogle(muid);
        domain.updateModules(ident);
    }//GEN-LAST:event_btnOFFActionPerformed

    private void btnONActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnONActionPerformed
        if (this.btnOFF.isSelected()) {
            this.btnON.setSelected(true);
            this.btnOFF.setSelected(false);
        }
        this.domain.scheduler(ident).toogle(muid);
        domain.updateModules(ident);
    }//GEN-LAST:event_btnONActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btnOFF;
    private javax.swing.JToggleButton btnON;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables

    private void initStatus() {
        String status = this.domain.scheduler(ident).statetable().get(muid);
        if (status.equalsIgnoreCase("ON")) {
            this.btnON.setSelected(true);
            this.btnOFF.setSelected(false);
        } else {
            this.btnON.setSelected(false);
            this.btnOFF.setSelected(true);
        }


    }
}
